<?php
class CategoriesController extends AppController {
	var $name = 'Categories';
	var $helpers = array('Lilcart.Lilcart', 'Lilcart.Sanitize', 'Html', 'Form');
	/*var $actsAs = array('ImageUpload'=>array(
			'dirFormat' => '{$class}',
			'overwriteExisting' => false,
			'mustUploadFile' => false,
			'fileField' => 'image',
			'versions' => array(
				'thumb' => array(
					'vBaseDir' => '{IMAGES}',
					'vDirFormat' => '{$class}',
					'vFileFormat' => '{$filename}',
					'callback' => array('resize', 100, 100)
				),
				'large' => array(
					'callback' => array('resize', 600, 400)
				),
			)
	));*/
	
	function admin_index() {
		// always display in default language
		$this->Category->locale = Configure::read('LilcartPlugin.languages');
		$this->Category->recursive = 0;
		$this->set('data', $this->Category->generateTreeList());
	}
	
	function admin_add() {
		$this->setAction('admin_edit');
	}
	
	function admin_edit($id = null) {
		$lang = @$this->params['named']['lang'];
		$this->Category->locale = Configure::read('LilcartPlugin.languages');
		
		if (!empty($this->data)) {
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('The Category has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
			}
		} else if (is_numeric($id)) {
			// read acording to locale
			$this->data = $this->Category->read(null, $id);
			
			// set existing language
			if (!empty($lang)) {
				if ($title = Set::extract('/Title[locale='.$lang.']/content', $this->data))
					$this->data['Category']['title'] = $title[0];
					
				if ($descript = Set::extract('/Descript[locale='.$lang.']/content', $this->data))
					$this->data['Category']['descript'] = $descript[0];
			}
		}
		
		$this->set('_lang', $lang);
		$this->set('parentCategories', $this->Category->generateTreeList());
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $data = $this->Category->findById($id)) {
			$this->Category->delete($id);
			$this->Session->setFlash(__('Category has been deleted.', true));
			$this->redirect(array('action'=>'admin_index'));
		} else {
			$this->error404();
		}
	}
	
	function admin_up($id = null) {
		if (is_numeric($id) && $data = $this->Category->findById($id)) {
			$this->Category->moveUp($id);
			$this->Session->setFlash(__('Category has been moved up.', true));
			$this->redirect(array('action'=>'admin_index'));
		} else {
			$this->error404();
		}
	}
	
	function admin_down($id = null) {
		if (is_numeric($id) && $data = $this->Category->findById($id)) {
			$this->Category->moveDown($id);
			$this->Session->setFlash(__('Category has been moved down.', true));
			$this->redirect(array('action'=>'admin_index'));
		} else {
			$this->error404();
		}
	}
	
	/*function thumb($id, $name = null, $size = null) {
		Configure::write('debug', 2);
		$this->Attachment->recursive = -1;
		$correctSlug = false;
		if (is_numeric($id)) {
			$correctSlug = true;
			$row = $this->Attachment->read(null, $id);
		} else {
			$params = func_get_args();
			$file = array_pop($params);
			$folder = implode($params, '/');
			$extension = array_pop(explode('.', $file));
			if (strpos($extension, '_') !== false) {
				list($extension_, $size) = explode('_', $extension);
				$file = str_replace($extension, $extension_, $file);
				$extension = $extension_;
			}
			$conditions['dir'] = $folder;
			$conditions['filename'] = $file;
			$conditions['ext'] = $extension;
			$row = $this->Attachment->find('first', compact('conditions'));
		}
		if (!$row) {
			debug('No file found for ' . implode(func_get_args(), '/'));
			die;
		}
		extract ($row['Attachment']);
		if ($correctSlug) {
			$a_description = $this->Attachment->slug(${$this->Attachment->displayField});
			if (!$a_description) {
				$a_description = $filename;
				if (substr($a_description, -3) != $ext) {
					$a_description .= '.' . $ext;
				}
			} else {
				$a_description = str_replace('-' . $ext, '.' . $ext, $a_description);
				if (strpos('.' . $ext, $a_description) === false) {
					$a_description .= '.' . $ext;
				}
			}
			if ($name != $a_description) {
				$this->redirect(array($id, $a_description, $size), 301);
			}
		}
		$data = compact('modified');
		$file_path = APP . 'uploads' . DS . $dir . DS . $filename;
		if ($size) {
			$file_path .= '_' . $size;
		}
		if (!file_exists($file_path)) {
			$filename =  'missing.png';
			$ext = 'png';
			$data['path'] = 'uploads' . DS;
		} else {
			$data['path'] = 'uploads' . DS . $dir . DS;
		}
		$data['id'] = $filename;
		$data['extension'] = $ext;
		$data['download'] = isset($this->params['named']['download'])?$this->params['named']['download']:false;
		$data['name'] = $a_description;
		$data['size'] = $size;
		if ($this->publicAccess) {
			$data['cache'] = '+ 99days';
		}
		$this->set($data);
		$this->view = 'Media';
		$this->render();
		Configure::write('debug', 0);
	}*/
}
?>